<template>
  <div>
    <vxe-toolbar id="unitId" :setting="{storage: true}" export :refresh="{query: findList}">
      <template v-slot:buttons>
        <el-button @click="funcFilter">查询</el-button>
        <el-button @click="funcAdd">新增</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      highlight-current-row
      highlight-hover-row
      row-id="id"
      height="700"
      :loading="loading"
      :data="tableData"
      remote-sort
      @sort-change="sortList"
    >
      <vxe-table-column>
        <vxe-table-column field="partyGroupName" title="维护机构" min-width="200" sortable />
        <template v-slot:header>
          <el-select v-model="listQuery.partyGroupCode" placeholder="请选择维护机构" clearable style="width: 100%">
            <el-option
              v-for="item in partyGroup"
              :key="item.code"
              :label="item.groupName"
              :value="item.code"
            />
          </el-select>
        </template>
      </vxe-table-column>

      <vxe-table-column>
        <vxe-table-column field="name" title="名称" min-width="100" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.name" placeholder="请输入名称" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="type"
          title="类型"
          min-width="100"
          :formatter="formatterType"
          sortable
        />
        <template v-slot:header>
          <el-select v-model="listQuery.type" placeholder="请选择类型" style="width: 100%" clearable>
            <el-option value="0" label="单位" />
            <el-option value="1" label="辅助单位" />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column title="操作" fixed="right" width="130">
        <template slot-scope="scope">
          <el-tooltip v-if="scope.row.partyGroupCode == party.branchCode || branch.level == 0"  class="item" effect="dark" content="编辑" placement="top">
            <vxe-button type="text" @click="funcEdit(scope.row.code)">
              <i class="el-icon-edit" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip v-if="scope.row.partyGroupCode == party.branchCode || branch.level == 0"  class="item" effect="dark" content="删除" placement="top">
            <vxe-button type="text" @click="funcDelete(scope.row.code)">
              <i class="el-icon-delete" />
            </vxe-button>
          </el-tooltip>
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />

    <vxe-modal v-model="modelAddFlag" title="新增" width="500" height="280" resize>
      <compAdd ref="addRef" @fatherAddMethod="fatherAdd" @fatherCancelMethod="fatherCancel" />
    </vxe-modal>
    <vxe-modal v-model="modelEditFlag" title="编辑" width="500" height="280" resize>
      <compEdit ref="editRef" @fatherEditMethod="fatherEdit" @fatherCancelMethod="fatherCancel" />
    </vxe-modal>
  </div>
</template>
<script>
import Vue from "vue";
import "xe-utils";
import VXETable from "vxe-table";
import "vxe-table/lib/index.css";
import XEUtils from "xe-utils";
import VXEUtils from "vxe-utils";
Vue.use(VXETable, VXEUtils, XEUtils, { mounts: ["cookie"] });
import compAdd from "./components/unitAdd.vue";
import compEdit from "./components/unitEdit.vue";
import compDetail from "./components/unitDetail.vue";
import request from "@/utils/request";
export default {
  name: "UnitList",
  components: {
    compAdd,
    compEdit,
    compDetail
  },
  data() {
    return {
      branch: this.$store.getters.branch,
      party: this.$store.getters.party,
      message: "",
      loading: false,
      tableData: [],
      modelAddFlag: false,
      modelEditFlag: false,
      partyGroup:[],
      modelDetailFlag: false,
      copy: false,
      listQuery: {
        code: "",
        name: "",
        type: "",
        pageNum: 1,
        pageSize: 10,
        partyGroupCode:'',
        sortField: "",
        sortType: "",
        total: 0
      },
      // 所属类型
      typeList: []
    };
  },
  created() {
    // this.findTypeList();
    this.getPartyGroup()

  },
  methods: {
    //获取机构信息
    getPartyGroup() {
      request({
        url: '/arpa-basic-api/partyGroup/allPartyGroup',
        method: 'get'
      }).then(response => {
        this.partyGroup = response.data
      }).then(response => {
        this.findList()
      })
    },

    // // 查询类型
    // findTypeList() {
    //   request({
    //     url: `/arpa-basic-api/dict`,
    //     method: "get",
    //     params: { type: "unitType" }
    //   }).then(response => {
    //     this.typeList = response.data.records;
    //     this.findList();
    //   });
    // },
    // 翻译类型名称
    formatterType({ cellValue }) {
      // const item = this.typeList.find(item => Number(item.code) === cellValue);
      return cellValue===0 ? "单位" : "辅助单位";
    },
    findList() {
      this.loading = true;
      return new Promise(resolve => {
        request({
          url: "/arpa-basic-api/unit",
          method: "get",
          params: this.listQuery
        }).then(response => {
          this.tableData = response.data.records;
          this.listQuery.total = response.data.total;
          this.loading = false;
          resolve();
        });
      });
    },
    // 查询
    funcFilter() {
      this.listQuery.pageNum = 1;
      this.findList();
    },
    // 排序
    sortList({ column, property, order }) {
      this.listQuery.sortField = property;
      this.listQuery.sortType = order;
      this.findList();
    },
    // 添加方法
    funcAdd() {
      this.$refs.addRef.reset();    //重置

      this.modelAddFlag = true;
      // this.$refs.addRef.partyGroup = this.partyGroup

    },
    // 修改方法
    funcEdit(code) {
      this.$refs.editRef.getInfo(code);
      // this.$refs.editRef.partyGroup = this.partyGroup
      this.modelEditFlag = true;
    },
    // 详情方法
    funcDetail(code) {
      this.$refs.detailRef.getInfo(code);
      this.modelDetailFlag = true;
    },
    // 删除方法
    funcDelete(row) {
      this.$confirm("确认删除吗？")
        .then(_ => {
          request({
            url: `/arpa-basic-api/unit`,
            method: "delete",
            params: { codes: row }
          }).then(response => {
            this.$message({ message: response.msg, type: "success" });
            this.findList();
          });
        })
        .catch(_ => {});
    },
    // 增加组件的父方法
    fatherAdd() {
      this.modelAddFlag = false;
      this.findList();
    },
    // 编辑组件的父方法
    fatherEdit() {
      this.modelEditFlag = false;
      this.findList();
    },
    // 公共取消
    fatherCancel(modelFlag) {
      switch (modelFlag) {
        case "add":
          this.modelAddFlag = false;
          break;
        case "edit":
          this.modelEditFlag = false;
          break;
        case "detail":
          this.modelDetailFlag = false;
          break;
        default:
      }
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage;
      this.listQuery.pageSize = pageSize;
      this.findList();
    },
    // 底部颜色设置
    /*  footerCellClassName({ $rowIndex, column, columnIndex }) {
      if (columnIndex === 0) {
        if ($rowIndex === 0) {
          return 'col-blue'
        } else {
          return 'col-red'
        }
      }
    }, */
    // 设置底部合计
    footerMethod({ columns, data }) {
      return [
        columns.map((column, columnIndex) => {
          if (columnIndex === 0) {
            return "合计";
          }
          if (["pageviews", "forecast"].includes(column.property)) {
            return XEUtils.sum(data, column.property);
          }
          return null;
        })
      ];
    }
  }
};
</script>

<style>
.vxe-table .vxe-body--column:not(.col--ellipsis),
.vxe-table .vxe-footer--column:not(.col--ellipsis),
.vxe-table .vxe-header--column:not(.col--ellipsis) {
  padding: 6px 0;
}
.vxe-toolbar {
  padding: 0 0 10px 0;
}
</style>
